//
//  AWNoticeMessageResolver.m
//  AtWork2
//
//  Created by lucas on 15/6/29.
//  Copyright (c) 2015年 Foreveross. All rights reserved.
//

#import "AWNoticeMessageResolver.h"
#import "AWMessage.h"
#import "AWNoticeMessage.h"
#import "AWDiscussionNoticeMessage.h"
#import "AWP2PNoticeMessage.h"
#import "AWSystemNoticeMessage.h"

@implementation AWNoticeMessageResolver

+ (AWNoticeMessage *)resolveIfPossibleWithDictionary:(NSDictionary *)message {
    
    BOOL isSystemMessage = [[message[AWMessageKFrom] uppercaseString] isEqualToString:AWMessageKSystem];
    if (nil == message[AWMessageKBody][AWMessageKType] && !isSystemMessage) {
        return nil;
    }
    
    AWNoticeMessageType noticeMessageType = parseNoticeMessageType(message[AWMessageKBody][AWMessageKType]);
    noticeMessageType = noticeMessageType == NSNotFound && isSystemMessage ?AWNoticeMessageTypeSystem:noticeMessageType;
    switch (noticeMessageType) {
        case AWNoticeMessageTypeP2P:
            return [self resolveToP2PNoticeMessageWithDictionary:message];
        case AWNoticeMessageTypeDiscussion:
            return [self resolveToDiscussionNoticeMessageWithDictionary:message];
        case AWNoticeMessageTypeSystem:
            return [self resolveToSystemNoticeMessageWithDictionary:message];

    }
}

+ (AWNoticeMessage *)resolveToSystemNoticeMessageWithDictionary:(NSDictionary *)message {
    AWSystemNoticeMessage *noticeMessage = [AWSystemNoticeMessage messageWithDictionary:message];
    noticeMessage.noticeMessageType = AWNoticeMessageTypeSystem;
    return noticeMessage;
}

+ (AWNoticeMessage *)resolveToDiscussionNoticeMessageWithDictionary:(NSDictionary *)message {
    AWNoticeMessage *noticeMessage = [AWDiscussionNoticeMessage messageWithDictionary:message];
    noticeMessage.noticeMessageType = AWNoticeMessageTypeDiscussion;
    return noticeMessage;
}

+ (AWNoticeMessage *)resolveToP2PNoticeMessageWithDictionary:(NSDictionary *)message {
    AWNoticeMessage *noticeMessage = [AWP2PNoticeMessage messageWithDictionary:message];
    noticeMessage.noticeMessageType = AWNoticeMessageTypeP2P;
    return noticeMessage;
}

@end
